import json
import os
from utils.log import logger
# 获取父目录的绝对路径
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
class Sel_Package():
    def __init__(self):
        pass
    def list_to_dict(self):
        with open(parent_dir+'/data/data_dict.json', encoding='utf-8') as f:
            data_list = json.load(f)
        alias_list = data_list[0]['header']#读取data_dict中header列表
        alias_dict={}
        #转换成header对应的字典，如{“ID”:{alias:"",id：”“，   }}
        for alias in alias_list:
            alias_name=alias["alias"]
            alias_dict[alias_name]=alias
        return alias_dict


    #封装sel模块
    def select_package(self,query,colunm,alias_dict):
        output={}
        output["query"]=query
        sel=[]
        for item in colunm:
            try:
                item_sel = {}
                item_sel["id"] = alias_dict[item]["id"]
                item_sel["name"] = alias_dict[item]["alias"]
                item_sel["type"] = alias_dict[item]["type"]
                if "timeLevel" in alias_dict[item]:
                    item_sel["timeLevel"] = alias_dict[item]["timeLevel"]
                else:
                    item_sel["timeLevel"] = ""
                if "member" in alias_dict[item]:
                    item_sel["member_amount"]=(alias_dict[item]["member"])
                else:
                    item_sel["member_amount"]=0
            except Exception as err:
                logger.error("some errors happened in select_package[{}]: {}".format(type(err),str(err)))
                raise
            sel.append(item_sel)
            # Find the corresponding item in dict.json
            output["sel"] = sel
        return output


